
<!doctype html>
<html lang="en" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
        <meta name="description" content="文档">
      
      
        <meta name="author" content="happyhelloworld">
      
      
        <link rel="canonical" href="http://happyhelloworld.gitee.io/blog/monitor/log-monitor/promtail%2Bloki%2Bgrafana%E6%97%A5%E5%BF%97%E7%9B%91%E6%8E%A7%E7%B3%BB%E7%BB%9F/">
      
      
        <link rel="prev" href="../../monitor/">
      
      
        <link rel="next" href="../../../tools/tools/">
      
      <link rel="icon" href="../../../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.4.2, mkdocs-material-9.1.6">
    
    
      
        <title>loki日志监控 - Mkdocs博客分享</title>
      
    
    
      <link rel="stylesheet" href="../../../assets/stylesheets/main.ded33207.min.css">
      
        
        <link rel="stylesheet" href="../../../assets/stylesheets/palette.a0c5b2b5.min.css">
      
      
  
  
    
    
  
    
    
  
    
    
  
    
    
  
    
    
  
    
    
  
    
    
  
    
    
  
    
    
  
    
    
  
    
    
  
    
    
  
  
  <style>:root{--md-admonition-icon--note:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M1 7.775V2.75C1 1.784 1.784 1 2.75 1h5.025c.464 0 .91.184 1.238.513l6.25 6.25a1.75 1.75 0 0 1 0 2.474l-5.026 5.026a1.75 1.75 0 0 1-2.474 0l-6.25-6.25A1.752 1.752 0 0 1 1 7.775Zm1.5 0c0 .066.026.13.073.177l6.25 6.25a.25.25 0 0 0 .354 0l5.025-5.025a.25.25 0 0 0 0-.354l-6.25-6.25a.25.25 0 0 0-.177-.073H2.75a.25.25 0 0 0-.25.25ZM6 5a1 1 0 1 1 0 2 1 1 0 0 1 0-2Z"/></svg>');--md-admonition-icon--abstract:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M2.5 1.75v11.5c0 .138.112.25.25.25h3.17a.75.75 0 0 1 0 1.5H2.75A1.75 1.75 0 0 1 1 13.25V1.75C1 .784 1.784 0 2.75 0h8.5C12.216 0 13 .784 13 1.75v7.736a.75.75 0 0 1-1.5 0V1.75a.25.25 0 0 0-.25-.25h-8.5a.25.25 0 0 0-.25.25Zm13.274 9.537v-.001l-4.557 4.45a.75.75 0 0 1-1.055-.008l-1.943-1.95a.75.75 0 0 1 1.062-1.058l1.419 1.425 4.026-3.932a.75.75 0 1 1 1.048 1.074ZM4.75 4h4.5a.75.75 0 0 1 0 1.5h-4.5a.75.75 0 0 1 0-1.5ZM4 7.75A.75.75 0 0 1 4.75 7h2a.75.75 0 0 1 0 1.5h-2A.75.75 0 0 1 4 7.75Z"/></svg>');--md-admonition-icon--info:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"/></svg>');--md-admonition-icon--tip:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M3.499.75a.75.75 0 0 1 1.5 0v.996C5.9 2.903 6.793 3.65 7.662 4.376l.24.202c-.036-.694.055-1.422.426-2.163C9.1.873 10.794-.045 12.622.26 14.408.558 16 1.94 16 4.25c0 1.278-.954 2.575-2.44 2.734l.146.508.065.22c.203.701.412 1.455.476 2.226.142 1.707-.4 3.03-1.487 3.898C11.714 14.671 10.27 15 8.75 15h-6a.75.75 0 0 1 0-1.5h1.376a4.484 4.484 0 0 1-.563-1.191 3.835 3.835 0 0 1-.05-2.063 4.647 4.647 0 0 1-2.025-.293.75.75 0 0 1 .525-1.406c1.357.507 2.376-.006 2.698-.318l.009-.01a.747.747 0 0 1 1.06 0 .748.748 0 0 1-.012 1.074c-.912.92-.992 1.835-.768 2.586.221.74.745 1.337 1.196 1.621H8.75c1.343 0 2.398-.296 3.074-.836.635-.507 1.036-1.31.928-2.602-.05-.603-.216-1.224-.422-1.93l-.064-.221c-.12-.407-.246-.84-.353-1.29a2.425 2.425 0 0 1-.507-.441 3.075 3.075 0 0 1-.633-1.248.75.75 0 0 1 1.455-.364c.046.185.144.436.31.627.146.168.353.305.712.305.738 0 1.25-.615 1.25-1.25 0-1.47-.95-2.315-2.123-2.51-1.172-.196-2.227.387-2.706 1.345-.46.92-.27 1.774.019 3.062l.042.19a.884.884 0 0 1 .01.05c.348.443.666.949.94 1.553a.75.75 0 1 1-1.365.62c-.553-1.217-1.32-1.94-2.3-2.768L6.7 5.527c-.814-.68-1.75-1.462-2.692-2.619a3.737 3.737 0 0 0-1.023.88c-.406.495-.663 1.036-.722 1.508.116.122.306.21.591.239.388.038.797-.06 1.032-.19a.75.75 0 0 1 .728 1.31c-.515.287-1.23.439-1.906.373-.682-.067-1.473-.38-1.879-1.193L.75 5.677V5.5c0-.984.48-1.94 1.077-2.664.46-.559 1.05-1.055 1.673-1.353V.75Z"/></svg>');--md-admonition-icon--success:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M13.78 4.22a.75.75 0 0 1 0 1.06l-7.25 7.25a.75.75 0 0 1-1.06 0L2.22 9.28a.751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018L6 10.94l6.72-6.72a.75.75 0 0 1 1.06 0Z"/></svg>');--md-admonition-icon--question:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.92 6.085h.001a.749.749 0 1 1-1.342-.67c.169-.339.436-.701.849-.977C6.845 4.16 7.369 4 8 4a2.756 2.756 0 0 1 1.637.525c.503.377.863.965.863 1.725 0 .448-.115.83-.329 1.15-.205.307-.47.513-.692.662-.109.072-.22.138-.313.195l-.006.004a6.24 6.24 0 0 0-.26.16.952.952 0 0 0-.276.245.75.75 0 0 1-1.248-.832c.184-.264.42-.489.692-.661.103-.067.207-.132.313-.195l.007-.004c.1-.061.182-.11.258-.161a.969.969 0 0 0 .277-.245C8.96 6.514 9 6.427 9 6.25a.612.612 0 0 0-.262-.525A1.27 1.27 0 0 0 8 5.5c-.369 0-.595.09-.74.187a1.01 1.01 0 0 0-.34.398ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"/></svg>');--md-admonition-icon--warning:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"/></svg>');--md-admonition-icon--failure:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M2.344 2.343h-.001a8 8 0 0 1 11.314 11.314A8.002 8.002 0 0 1 .234 10.089a8 8 0 0 1 2.11-7.746Zm1.06 10.253a6.5 6.5 0 1 0 9.108-9.275 6.5 6.5 0 0 0-9.108 9.275ZM6.03 4.97 8 6.94l1.97-1.97a.749.749 0 0 1 1.275.326.749.749 0 0 1-.215.734L9.06 8l1.97 1.97a.749.749 0 0 1-.326 1.275.749.749 0 0 1-.734-.215L8 9.06l-1.97 1.97a.749.749 0 0 1-1.275-.326.749.749 0 0 1 .215-.734L6.94 8 4.97 6.03a.751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018Z"/></svg>');--md-admonition-icon--danger:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M9.504.43a1.516 1.516 0 0 1 2.437 1.713L10.415 5.5h2.123c1.57 0 2.346 1.909 1.22 3.004l-7.34 7.142a1.249 1.249 0 0 1-.871.354h-.302a1.25 1.25 0 0 1-1.157-1.723L5.633 10.5H3.462c-1.57 0-2.346-1.909-1.22-3.004L9.503.429Zm1.047 1.074L3.286 8.571A.25.25 0 0 0 3.462 9H6.75a.75.75 0 0 1 .694 1.034l-1.713 4.188 6.982-6.793A.25.25 0 0 0 12.538 7H9.25a.75.75 0 0 1-.683-1.06l2.008-4.418.003-.006a.036.036 0 0 0-.004-.009l-.006-.006-.008-.001c-.003 0-.006.002-.009.004Z"/></svg>');--md-admonition-icon--bug:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M4.72.22a.75.75 0 0 1 1.06 0l1 .999a3.488 3.488 0 0 1 2.441 0l.999-1a.748.748 0 0 1 1.265.332.75.75 0 0 1-.205.729l-.775.776c.616.63.995 1.493.995 2.444v.327c0 .1-.009.197-.025.292.408.14.764.392 1.029.722l1.968-.787a.75.75 0 0 1 .556 1.392L13 7.258V9h2.25a.75.75 0 0 1 0 1.5H13v.5c0 .409-.049.806-.141 1.186l2.17.868a.75.75 0 0 1-.557 1.392l-2.184-.873A4.997 4.997 0 0 1 8 16a4.997 4.997 0 0 1-4.288-2.427l-2.183.873a.75.75 0 0 1-.558-1.392l2.17-.868A5.036 5.036 0 0 1 3 11v-.5H.75a.75.75 0 0 1 0-1.5H3V7.258L.971 6.446a.75.75 0 0 1 .558-1.392l1.967.787c.265-.33.62-.583 1.03-.722a1.677 1.677 0 0 1-.026-.292V4.5c0-.951.38-1.814.995-2.444L4.72 1.28a.75.75 0 0 1 0-1.06Zm.53 6.28a.75.75 0 0 0-.75.75V11a3.5 3.5 0 1 0 7 0V7.25a.75.75 0 0 0-.75-.75ZM6.173 5h3.654A.172.172 0 0 0 10 4.827V4.5a2 2 0 1 0-4 0v.327c0 .096.077.173.173.173Z"/></svg>');--md-admonition-icon--example:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M5 5.782V2.5h-.25a.75.75 0 0 1 0-1.5h6.5a.75.75 0 0 1 0 1.5H11v3.282l3.666 5.76C15.619 13.04 14.543 15 12.767 15H3.233c-1.776 0-2.852-1.96-1.899-3.458Zm-2.4 6.565a.75.75 0 0 0 .633 1.153h9.534a.75.75 0 0 0 .633-1.153L12.225 10.5h-8.45ZM9.5 2.5h-3V6c0 .143-.04.283-.117.403L4.73 9h6.54L9.617 6.403A.746.746 0 0 1 9.5 6Z"/></svg>');--md-admonition-icon--quote:url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M1.75 2.5h10.5a.75.75 0 0 1 0 1.5H1.75a.75.75 0 0 1 0-1.5Zm4 5h8.5a.75.75 0 0 1 0 1.5h-8.5a.75.75 0 0 1 0-1.5Zm0 5h8.5a.75.75 0 0 1 0 1.5h-8.5a.75.75 0 0 1 0-1.5ZM2.5 7.75v6a.75.75 0 0 1-1.5 0v-6a.75.75 0 0 1 1.5 0Z"/></svg>');}</style>


    
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
    <script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      
  


  
  


  
    <script>"undefined"!=typeof __md_analytics&&__md_analytics()</script>
  

    
    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="teal" data-md-color-accent="">
  
    
    
      <script>var palette=__md_get("__palette");if(palette&&"object"==typeof palette.color)for(var key of Object.keys(palette.color))document.body.setAttribute("data-md-color-"+key,palette.color[key])</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#_1" class="md-skip">
          Skip to content
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
    
      

  

<header class="md-header md-header--shadow md-header--lifted" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="Header">
    <a href="../../.." title="Mkdocs博客分享" class="md-header__button md-logo" aria-label="Mkdocs博客分享" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            Mkdocs博客分享
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              loki日志监控
            
          </span>
        </div>
      </div>
    </div>
    
      <form class="md-header__option" data-md-component="palette">
        
          
          <input class="md-option" data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme="default" data-md-color-primary="teal" data-md-color-accent=""  aria-label="Switch to light mode"  type="radio" name="__palette" id="__palette_1">
          
            <label class="md-header__button md-icon" title="Switch to light mode" for="__palette_2" hidden>
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m17.75 4.09-2.53 1.94.91 3.06-2.63-1.81-2.63 1.81.91-3.06-2.53-1.94L12.44 4l1.06-3 1.06 3 3.19.09m3.5 6.91-1.64 1.25.59 1.98-1.7-1.17-1.7 1.17.59-1.98L15.75 11l2.06-.05L18.5 9l.69 1.95 2.06.05m-2.28 4.95c.83-.08 1.72 1.1 1.19 1.85-.32.45-.66.87-1.08 1.27C15.17 23 8.84 23 4.94 19.07c-3.91-3.9-3.91-10.24 0-14.14.4-.4.82-.76 1.27-1.08.75-.53 1.93.36 1.85 1.19-.27 2.86.69 5.83 2.89 8.02a9.96 9.96 0 0 0 8.02 2.89m-1.64 2.02a12.08 12.08 0 0 1-7.8-3.47c-2.17-2.19-3.33-5-3.49-7.82-2.81 3.14-2.7 7.96.31 10.98 3.02 3.01 7.84 3.12 10.98.31Z"/></svg>
            </label>
          
        
          
          <input class="md-option" data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme="slate" data-md-color-primary="" data-md-color-accent=""  aria-label="Switch to dark mode"  type="radio" name="__palette" id="__palette_2">
          
            <label class="md-header__button md-icon" title="Switch to dark mode" for="__palette_1" hidden>
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 7a5 5 0 0 1 5 5 5 5 0 0 1-5 5 5 5 0 0 1-5-5 5 5 0 0 1 5-5m0 2a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0-7 2.39 3.42C13.65 5.15 12.84 5 12 5c-.84 0-1.65.15-2.39.42L12 2M3.34 7l4.16-.35A7.2 7.2 0 0 0 5.94 8.5c-.44.74-.69 1.5-.83 2.29L3.34 7m.02 10 1.76-3.77a7.131 7.131 0 0 0 2.38 4.14L3.36 17M20.65 7l-1.77 3.79a7.023 7.023 0 0 0-2.38-4.15l4.15.36m-.01 10-4.14.36c.59-.51 1.12-1.14 1.54-1.86.42-.73.69-1.5.83-2.29L20.64 17M12 22l-2.41-3.44c.74.27 1.55.44 2.41.44.82 0 1.63-.17 2.37-.44L12 22Z"/></svg>
            </label>
          
        
      </form>
    
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
      </label>
      <div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="Search" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="Search">
        
          <a href="javascript:void(0)" class="md-search__icon md-icon" title="Share" aria-label="Share" data-clipboard data-clipboard-text="" data-md-component="search-share" tabindex="-1">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M18 16.08c-.76 0-1.44.3-1.96.77L8.91 12.7c.05-.23.09-.46.09-.7 0-.24-.04-.47-.09-.7l7.05-4.11c.54.5 1.25.81 2.04.81a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3c0 .24.04.47.09.7L8.04 9.81C7.5 9.31 6.79 9 6 9a3 3 0 0 0-3 3 3 3 0 0 0 3 3c.79 0 1.5-.31 2.04-.81l7.12 4.15c-.05.21-.08.43-.08.66 0 1.61 1.31 2.91 2.92 2.91 1.61 0 2.92-1.3 2.92-2.91A2.92 2.92 0 0 0 18 16.08Z"/></svg>
          </a>
        
        <button type="reset" class="md-search__icon md-icon" title="Clear" aria-label="Clear" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg>
        </button>
      </nav>
      
        <div class="md-search__suggest" data-md-component="search-suggest"></div>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            Initializing search
          </div>
          <ol class="md-search-result__list" role="presentation"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
      <div class="md-header__source">
        <a href="https://gitee.com/happyhelloworld/blog.git" title="Go to repository" class="md-source" data-md-component="source">
  <div class="md-source__icon md-icon">
    
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z"/></svg>
  </div>
  <div class="md-source__repository">
    gitee
  </div>
</a>
      </div>
    
  </nav>
  
    
      
<nav class="md-tabs" aria-label="Tabs" data-md-component="tabs">
  <div class="md-grid">
    <ul class="md-tabs__list">
      
        
  
  


  <li class="md-tabs__item">
    <a href="../../.." class="md-tabs__link">
      Home
    </a>
  </li>

      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../../linux/linux/" class="md-tabs__link">
        Linux
      </a>
    </li>
  

      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../../ansible/test-01/" class="md-tabs__link">
        Ansible
      </a>
    </li>
  

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../../python/python/" class="md-tabs__link">
      Python
    </a>
  </li>

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../../vue/vue/" class="md-tabs__link">
      Vue
    </a>
  </li>

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../../springcloud/springcloud/" class="md-tabs__link">
      SpringCloud
    </a>
  </li>

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../../go/go/" class="md-tabs__link">
      Go
    </a>
  </li>

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../../docker/docker/" class="md-tabs__link">
      Docker
    </a>
  </li>

      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../../k8s/k8s/" class="md-tabs__link">
        Kubernetes
      </a>
    </li>
  

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../../devops/devops/" class="md-tabs__link">
      DevOps
    </a>
  </li>

      
        
  
  
    
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../monitor/" class="md-tabs__link md-tabs__link--active">
        Monitor
      </a>
    </li>
  

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../../tools/tools/" class="md-tabs__link">
      Tools
    </a>
  </li>

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../../about/" class="md-tabs__link">
      About
    </a>
  </li>

      
    </ul>
  </div>
</nav>
    
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

  


  

<nav class="md-nav md-nav--primary md-nav--lifted md-nav--integrated" aria-label="Navigation" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../../.." title="Mkdocs博客分享" class="md-nav__button md-logo" aria-label="Mkdocs博客分享" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"/></svg>

    </a>
    Mkdocs博客分享
  </label>
  
    <div class="md-nav__source">
      <a href="https://gitee.com/happyhelloworld/blog.git" title="Go to repository" class="md-source" data-md-component="source">
  <div class="md-source__icon md-icon">
    
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z"/></svg>
  </div>
  <div class="md-source__repository">
    gitee
  </div>
</a>
    </div>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../.." class="md-nav__link">
        Home
      </a>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_2" >
      
      
        
          
        
      
      
        <label class="md-nav__link" for="__nav_2" id="__nav_2_label" tabindex="0">
          Linux
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_2_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_2">
          <span class="md-nav__icon md-icon"></span>
          Linux
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../linux/linux/" class="md-nav__link">
        入门
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_3" >
      
      
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
      
      
        <label class="md-nav__link" for="__nav_3" id="__nav_3_label" tabindex="0">
          Ansible
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_3_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_3">
          <span class="md-nav__icon md-icon"></span>
          Ansible
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../ansible/test-01/" class="md-nav__link">
        1. Ansible 介绍
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../ansible/test-02.md" class="md-nav__link">
        2. 安装 Ansible
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-03.md" class="md-nav__link">
        3. 快速开始
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-04.md" class="md-nav__link">
        4. 认识主机清单
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-05.md" class="md-nav__link">
        5. Patterns 匹配
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-06.md" class="md-nav__link">
        6. 使用 ad-hoc 命令
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-07.md" class="md-nav__link">
        7. 熟悉 ansible 命令
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-08.md" class="md-nav__link">
        8. 使用 Playbook
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-09.md" class="md-nav__link">
        9. 包含和角色
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-10.md" class="md-nav__link">
        10. 使用变量
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-11.md" class="md-nav__link">
        11. Facts 数据
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-12.md" class="md-nav__link">
        12. Jinja2 模板语法
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-13.md" class="md-nav__link">
        13. 条件判断与循环
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-14.md" class="md-nav__link">
        14. Blocks
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-15.md" class="md-nav__link">
        15. Playbook 高级特性
      </a>
    </li>
  

            
          
            
              
  
  
  
    
      
    
    <li class="md-nav__item md-nav__item--section md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_3_16" >
      
      
        
          
        
          
        
          
        
          
        
          
        
      
      
        <label class="md-nav__link" for="__nav_3_16" id="__nav_3_16_label" tabindex="0">
          扩展阅读(深入必读)
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="2" aria-labelledby="__nav_3_16_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_3_16">
          <span class="md-nav__icon md-icon"></span>
          扩展阅读(深入必读)
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-e01.md" class="md-nav__link">
        1. YAML 语法
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-e02.md" class="md-nav__link">
        2. Vaults 加密数据
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-e03.md" class="md-nav__link">
        3. 关键字使用
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-e04.md" class="md-nav__link">
        4. 模块索引
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../test-e05.md" class="md-nav__link">
        5. Ansible 配置
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../../python/python/" class="md-nav__link">
        Python
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../../vue/vue/" class="md-nav__link">
        Vue
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../../springcloud/springcloud/" class="md-nav__link">
        SpringCloud
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../../go/go/" class="md-nav__link">
        Go
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../../docker/docker/" class="md-nav__link">
        Docker
      </a>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_9" >
      
      
        
          
        
          
        
          
        
          
        
          
        
      
      
        <label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
          Kubernetes
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_9_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_9">
          <span class="md-nav__icon md-icon"></span>
          Kubernetes
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../k8s/k8s/" class="md-nav__link">
        入门
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../k8s/practise/" class="md-nav__link">
        实践
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../k8s/core/" class="md-nav__link">
        核心概念
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../k8s/maintain/" class="md-nav__link">
        运维指南
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../k8s/source/" class="md-nav__link">
        源码导读
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../../devops/devops/" class="md-nav__link">
        DevOps
      </a>
    </li>
  

    
      
      
      

  
  
    
  
  
    
    <li class="md-nav__item md-nav__item--active md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_11" checked>
      
      
        
          
        
          
        
      
      
        <label class="md-nav__link" for="__nav_11" id="__nav_11_label" tabindex="0">
          Monitor
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_11_label" aria-expanded="true">
        <label class="md-nav__title" for="__nav_11">
          <span class="md-nav__icon md-icon"></span>
          Monitor
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../monitor/" class="md-nav__link">
        介绍
      </a>
    </li>
  

            
          
            
              
  
  
    
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
      
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          loki日志监控
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="./" class="md-nav__link md-nav__link--active">
        loki日志监控
      </a>
      
        

<nav class="md-nav md-nav--secondary" aria-label="Table of contents">
  
  
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      Table of contents
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    主旨
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    说明
  </a>
  
    <nav class="md-nav" aria-label="说明">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#1" class="md-nav__link">
    1、目录结构
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#2" class="md-nav__link">
    2、环境说明
  </a>
  
    <nav class="md-nav" aria-label="2、环境说明">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    虚拟机环境
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    容器环境
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#k8s" class="md-nav__link">
    k8s环境
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#3" class="md-nav__link">
    3、任务分解
  </a>
  
    <nav class="md-nav" aria-label="3、任务分解">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#promtaillokigrafana" class="md-nav__link">
    虚拟机服务器搭建Promtail+Loki+Grafana轻量日志采集
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#dockerpromtaillokigrafana" class="md-nav__link">
    Docker搭建Promtail+Loki+Grafana轻量日志采集
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#k8spromtaillokigrafana" class="md-nav__link">
    k8s搭建搭建Promtail+Loki+Grafana轻量日志采集
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_5" class="md-nav__link">
    数据存储配置优化
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_6" class="md-nav__link">
    增加告警配置
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_7" class="md-nav__link">
    方案优化
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_8" class="md-nav__link">
    环境部署
  </a>
  
    <nav class="md-nav" aria-label="环境部署">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#loki" class="md-nav__link">
    虚拟机部署loki
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#dockerloki" class="md-nav__link">
    Docker方式部署loki
  </a>
  
    <nav class="md-nav" aria-label="Docker方式部署loki">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#loki_1" class="md-nav__link">
    loki部署配置
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#promtail" class="md-nav__link">
    promtail部署配置
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#grafana" class="md-nav__link">
    grafana部署配置
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#loki-configyaml" class="md-nav__link">
    loki-config.yaml
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#promtail-configyaml" class="md-nav__link">
    promtail-config.yaml
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_9" class="md-nav__link">
    测试数据在面板展示
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#k8sloki" class="md-nav__link">
    测试环境k8s部署loki
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_10" class="md-nav__link">
    数据持久化配置
  </a>
  
    <nav class="md-nav" aria-label="数据持久化配置">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_11" class="md-nav__link">
    方案说明
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_12" class="md-nav__link">
    日志监控告警……待补充
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_13" class="md-nav__link">
    官网参考链接
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_14" class="md-nav__link">
    参考引用
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_15" class="md-nav__link">
    历史事件
  </a>
  
    <nav class="md-nav" aria-label="历史事件">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#2023-04-12" class="md-nav__link">
    2023-04-12｜容器、虚拟机轻量部署采集
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#2023-04-10-k8s" class="md-nav__link">
    2023-04-10 ｜k8s轻量部署采集
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#2023-04-08" class="md-nav__link">
    2023-04-08 ｜创建文档
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../../tools/tools/" class="md-nav__link">
        Tools
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../../about/" class="md-nav__link">
        About
      </a>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
          
          
            <div class="md-content" data-md-component="content">
              <article class="md-content__inner md-typeset">
                
                  


  <h1>loki日志监控</h1>

<h3 id="_1">主旨</h3>
<ul>
<li>基于Loki+promtail+grafana日志采集监控展示</li>
<li>部署配置使用配置解析</li>
<li>深入研究不断优化方案</li>
</ul>
<h3 id="_2">说明</h3>
<h4 id="1">1、目录结构</h4>
<div class="highlight"><pre><span></span><code>$ tree -LN 1
.
├── image       # 文档截图存放
├── loki_in_docker  # docker部署的存放路径
├── loki_in_k8s # k8s部署的存放路径
├── loki_in_vm  # 虚拟机部署的存放路径
└── promtail+loki+grafana日志监控系统.md  #主文档

4 directories, 1 file
</code></pre></div>
<h4 id="2">2、环境说明</h4>
<ul>
<li>
<h5 id="_3">虚拟机环境</h5>
</li>
</ul>
<div class="highlight"><pre><span></span><code>192.168.1.100   /usr/local/loki /usr/local/promtail
</code></pre></div>
<ul>
<li>
<h5 id="_4">容器环境</h5>
</li>
</ul>
<div class="highlight"><pre><span></span><code>192.168.1.102   /root/loki_in_docker
</code></pre></div>
<ul>
<li>
<h5 id="k8s">k8s环境</h5>
</li>
</ul>
<div class="highlight"><pre><span></span><code>192.168.1.100   /root/k8s/loki/loki_in_k8s
192.168.1.101
192.168.1.102
</code></pre></div>
<h4 id="3">3、任务分解</h4>
<ul>
<li>
<h5 id="promtaillokigrafana">虚拟机服务器搭建Promtail+Loki+Grafana轻量日志采集</h5>
</li>
<li>
<h5 id="dockerpromtaillokigrafana">Docker搭建Promtail+Loki+Grafana轻量日志采集</h5>
</li>
<li>
<h5 id="k8spromtaillokigrafana">k8s搭建搭建Promtail+Loki+Grafana轻量日志采集</h5>
</li>
<li>
<h5 id="_5">数据存储配置优化</h5>
</li>
<li>
<h5 id="_6">增加告警配置</h5>
</li>
<li>
<h5 id="_7">方案优化</h5>
</li>
</ul>
<h3 id="_8">环境部署</h3>
<h4 id="loki">虚拟机部署loki</h4>
<div class="highlight"><pre><span></span><code>## 这里的grafana我没有再安装，直接用下面容器部署的grafana
## 版本说明，配置文件链接
# ./loki-linux-amd64 -version
loki, version 2.5.0 (branch: HEAD, revision: 2d9d0ee23)
  build user:       root@4779f4b48f3a
  build date:       2022-04-07T21:50:00Z
  go version:       go1.17.6
  platform:         linux/amd64
# ll
总用量 53280
-rwxr-xr-x 1 root root 54554624 4月   8 2022 loki-linux-amd64
-rw-r--r-- 1 root root     1323 4月  12 12:10 loki-local-config.yaml 
# ./promtail-linux-amd64 -version
promtail, version 2.5.0 (branch: HEAD, revision: 2d9d0ee23)
  build user:       root@4779f4b48f3a
  build date:       2022-04-07T21:50:00Z
  go version:       go1.17.6
  platform:         linux/amd64
# ll
总用量 78352
-rwxr-xr-x 1 root root 80224856 4月   8 2022 promtail-linux-amd64
-rw-r--r-- 1 root root      313 4月  12 12:11 promtail-local-config.yaml

## 二进制文件下载地址，配置文件地址  
https://github.com/grafana/loki/releases/tag/v2.5.0  
https://github.com/grafana/loki/blob/v2.6.1/cmd/loki/loki-local-config.yaml
https://github.com/grafana/loki/blob/v2.5.0/clients/cmd/promtail/promtail-local-config.yaml
</code></pre></div>
<div class="highlight"><pre><span></span><code>## 启动测试
# pwd
/usr/local/loki
# ./loki-linux-amd64 -config.file=./loki-local-config.yaml
# pwd
/usr/local/promtail
# ./promtail-linux-amd64 -config.file=./promtail-local-config.yaml

## 若上面无日志报错，则可以执行后台启动
nohup ./loki-linux-amd64 -config.file=./loki-local-config.yaml &amp;
nohup ./promtail-linux-amd64 -config.file=./promtail-local-config.yaml &amp;
</code></pre></div>
<div class="highlight"><pre><span></span><code># ./loki-linux-amd64 -config.file=./loki-local-config.yaml
failed parsing config: ./loki-local-config.yaml: yaml: unmarshal errors:
  line 22: field embedded_cache not found in type cache.Config

该错误的原因是下载了v2.5.0配置文件，有问题，我换成v2.6.1启动OK
https://github.com/grafana/loki/blob/v2.6.1/cmd/loki/loki-local-config.yaml
</code></pre></div>
<p><img alt="vm-show-01" src="../image/loki-vm-grafana-show-01.png" /></p>
<p><img alt="vm-show-02" src="../image/loki-vm-grafana-show-02.png" /></p>
<div class="highlight"><pre><span></span><code>虚拟机，轻量日志采集展示完毕！
</code></pre></div>
<h4 id="dockerloki">Docker方式部署loki</h4>
<div class="highlight"><pre><span></span><code>搭建Promtail + Loki + Grafana 轻量日志系统
如下针对宿主机服务器/var/log/messages做采集展示
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="c1"># ip addr | grep ens | grep inet</span>
<span class="w">    </span>inet<span class="w"> </span><span class="m">192</span>.168.1.101/24<span class="w"> </span>brd<span class="w"> </span><span class="m">192</span>.168.1.255<span class="w"> </span>scope<span class="w"> </span>global<span class="w"> </span>ens160 
<span class="c1"># cat /etc/redhat-release </span>
CentOS<span class="w"> </span>Linux<span class="w"> </span>release<span class="w"> </span><span class="m">7</span>.9.2009<span class="w"> </span><span class="o">(</span>Core<span class="o">)</span>
<span class="c1"># pwd</span>
/root/loki_in_docker
<span class="c1"># ll</span>
总用量<span class="w"> </span><span class="m">12</span>
-rwxr-xr-x<span class="w"> </span><span class="m">1</span><span class="w"> </span>root<span class="w"> </span>root<span class="w">  </span><span class="m">488</span><span class="w"> </span>4月<span class="w">  </span><span class="m">12</span><span class="w"> </span><span class="m">10</span>:05<span class="w"> </span>install.sh<span class="w">     </span><span class="c1">## 基于如下配置，简单封装脚本</span>
-rw-r--r--<span class="w"> </span><span class="m">1</span><span class="w"> </span>root<span class="w"> </span>root<span class="w"> </span><span class="m">1298</span><span class="w"> </span>4月<span class="w">  </span><span class="m">12</span><span class="w"> </span><span class="m">10</span>:00<span class="w"> </span>loki-config.yaml<span class="w">       </span><span class="c1">## loki配置文件</span>
-rw-r--r--<span class="w"> </span><span class="m">1</span><span class="w"> </span>root<span class="w"> </span>root<span class="w">  </span><span class="m">302</span><span class="w"> </span>4月<span class="w">  </span><span class="m">12</span><span class="w"> </span><span class="m">10</span>:00<span class="w"> </span>promtail-config.yaml<span class="w">       </span><span class="c1">## promtail配置文件</span>
<span class="c1"># cat install.sh </span>
<span class="c1">#!/bin/bash</span>

<span class="c1">## Pull loki image and start loki container</span>
docker<span class="w"> </span>run<span class="w"> </span>--name<span class="w"> </span>loki<span class="w"> </span>-d<span class="w"> </span>-v<span class="w"> </span><span class="k">$(</span><span class="nb">pwd</span><span class="k">)</span>:/mnt/config<span class="w"> </span>-p<span class="w"> </span><span class="m">3100</span>:3100<span class="w"> </span>grafana/loki:2.7.4<span class="w"> </span>-config.file<span class="o">=</span>/mnt/config/loki-config.yaml

<span class="c1">## Pull promtail image and start promtail container</span>
docker<span class="w"> </span>run<span class="w"> </span>--name<span class="w"> </span>promtail<span class="w"> </span>-d<span class="w"> </span>-v<span class="w"> </span><span class="k">$(</span><span class="nb">pwd</span><span class="k">)</span>:/mnt/config<span class="w"> </span>-v<span class="w"> </span>/var/log:/var/log<span class="w"> </span>--link<span class="w"> </span>loki<span class="w"> </span>grafana/promtail:2.7.4<span class="w"> </span>-config.file<span class="o">=</span>/mnt/config/promtail-config.yaml

<span class="c1">## Pull grafana image and start grafana container</span>
docker<span class="w"> </span>run<span class="w"> </span>-d<span class="w"> </span>-p<span class="w"> </span><span class="m">3000</span>:3000<span class="w"> </span>grafana/grafana-oss
</code></pre></div>
<ul>
<li>
<h5 id="loki_1">loki部署配置</h5>
</li>
</ul>
<div class="highlight"><pre><span></span><code>wget<span class="w"> </span>https://raw.githubusercontent.com/grafana/loki/v2.7.4/cmd/loki/loki-local-config.yaml<span class="w"> </span>-O<span class="w"> </span>loki-config.yaml
</code></pre></div>
<div class="highlight"><pre><span></span><code>docker<span class="w"> </span>run<span class="w"> </span>--name<span class="w"> </span>loki<span class="w"> </span>-d<span class="w"> </span>-v<span class="w"> </span><span class="k">$(</span><span class="nb">pwd</span><span class="k">)</span>:/mnt/config<span class="w"> </span>-p<span class="w"> </span><span class="m">3100</span>:3100<span class="w"> </span>grafana/loki:2.7.4<span class="w"> </span>-config.file<span class="o">=</span>/mnt/config/loki-config.yaml
</code></pre></div>
<ul>
<li>
<h5 id="promtail">promtail部署配置</h5>
</li>
</ul>
<div class="highlight"><pre><span></span><code>wget<span class="w"> </span>https://raw.githubusercontent.com/grafana/loki/v2.7.4/clients/cmd/promtail/promtail-docker-config.yaml<span class="w"> </span>-O<span class="w"> </span>promtail-config.yaml
</code></pre></div>
<div class="highlight"><pre><span></span><code>docker<span class="w"> </span>run<span class="w"> </span>--name<span class="w"> </span>promtail<span class="w"> </span>-d<span class="w"> </span>-v<span class="w"> </span><span class="k">$(</span><span class="nb">pwd</span><span class="k">)</span>:/mnt/config<span class="w"> </span>-v<span class="w"> </span>/var/log:/var/log<span class="w"> </span>--link<span class="w"> </span>loki<span class="w"> </span>grafana/promtail:2.7.4<span class="w"> </span>-config.file<span class="o">=</span>/mnt/config/promtail-config.yaml
</code></pre></div>
<ul>
<li>
<h5 id="grafana">grafana部署配置</h5>
</li>
</ul>
<div class="highlight"><pre><span></span><code>docker<span class="w"> </span>run<span class="w"> </span>-d<span class="w"> </span>-p<span class="w"> </span><span class="m">3000</span>:3000<span class="w"> </span>grafana/grafana-oss
</code></pre></div>
<ul>
<li>
<h5 id="loki-configyaml">loki-config.yaml</h5>
</li>
</ul>
<div class="highlight"><pre><span></span><code><span class="c1"># cat loki-config.yaml </span>

<span class="nt">auth_enabled</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">false</span>

<span class="nt">server</span><span class="p">:</span>
<span class="w">  </span><span class="nt">http_listen_port</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">3100</span>
<span class="w">  </span><span class="nt">grpc_listen_port</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">9096</span>

<span class="nt">common</span><span class="p">:</span>
<span class="w">  </span><span class="nt">path_prefix</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">/tmp/loki</span>
<span class="w">  </span><span class="nt">storage</span><span class="p">:</span>
<span class="w">    </span><span class="nt">filesystem</span><span class="p">:</span>
<span class="w">      </span><span class="nt">chunks_directory</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">/tmp/loki/chunks</span>
<span class="w">      </span><span class="nt">rules_directory</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">/tmp/loki/rules</span>
<span class="w">  </span><span class="nt">replication_factor</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">1</span>
<span class="w">  </span><span class="nt">ring</span><span class="p">:</span>
<span class="w">    </span><span class="nt">instance_addr</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">127.0.0.1</span>
<span class="w">    </span><span class="nt">kvstore</span><span class="p">:</span>
<span class="w">      </span><span class="nt">store</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">inmemory</span>

<span class="nt">query_range</span><span class="p">:</span>
<span class="w">  </span><span class="nt">results_cache</span><span class="p">:</span>
<span class="w">    </span><span class="nt">cache</span><span class="p">:</span>
<span class="w">      </span><span class="nt">embedded_cache</span><span class="p">:</span>
<span class="w">        </span><span class="nt">enabled</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">true</span>
<span class="w">        </span><span class="nt">max_size_mb</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">100</span>

<span class="nt">schema_config</span><span class="p">:</span>
<span class="w">  </span><span class="nt">configs</span><span class="p">:</span>
<span class="w">    </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">from</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">2020-10-24</span>
<span class="w">      </span><span class="nt">store</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">boltdb-shipper</span>
<span class="w">      </span><span class="nt">object_store</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">filesystem</span>
<span class="w">      </span><span class="nt">schema</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">v11</span>
<span class="w">      </span><span class="nt">index</span><span class="p">:</span>
<span class="w">        </span><span class="nt">prefix</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">index_</span>
<span class="w">        </span><span class="nt">period</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">24h</span>

<span class="nt">ruler</span><span class="p">:</span>
<span class="w">  </span><span class="nt">alertmanager_url</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">http://localhost:9093</span>

<span class="c1"># By default, Loki will send anonymous, but uniquely-identifiable usage and configuration</span>
<span class="c1"># analytics to Grafana Labs. These statistics are sent to https://stats.grafana.org/</span>
<span class="c1">#</span>
<span class="c1"># Statistics help us better understand how Loki is used, and they show us performance</span>
<span class="c1"># levels for most users. This helps us prioritize features and documentation.</span>
<span class="c1"># For more information on what&#39;s sent, look at</span>
<span class="c1"># https://github.com/grafana/loki/blob/main/pkg/usagestats/stats.go</span>
<span class="c1"># Refer to the buildReport method to see what goes into a report.</span>
<span class="c1">#</span>
<span class="c1"># If you would like to disable reporting, uncomment the following lines:</span>
<span class="c1">#analytics:</span>
<span class="c1">#  reporting_enabled: false</span>
</code></pre></div>
<ul>
<li>
<h5 id="promtail-configyaml">promtail-config.yaml</h5>
</li>
</ul>
<div class="highlight"><pre><span></span><code><span class="c1"># cat promtail-config.yaml </span>

<span class="nt">server</span><span class="p">:</span>
<span class="w">  </span><span class="nt">http_listen_port</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">9080</span>
<span class="w">  </span><span class="nt">grpc_listen_port</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">0</span>

<span class="nt">positions</span><span class="p">:</span>
<span class="w">  </span><span class="nt">filename</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">/tmp/positions.yaml</span>

<span class="nt">clients</span><span class="p">:</span>
<span class="w">  </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">url</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">http://loki:3100/loki/api/v1/push</span>

<span class="nt">scrape_configs</span><span class="p">:</span>
<span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">job_name</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">system</span>
<span class="w">  </span><span class="nt">static_configs</span><span class="p">:</span>
<span class="w">  </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">targets</span><span class="p">:</span>
<span class="w">      </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">localhost</span>
<span class="w">    </span><span class="nt">labels</span><span class="p">:</span>
<span class="w">      </span><span class="nt">job</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">varlogs</span>
<span class="w">      </span><span class="nt">__path__</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">/var/log/messages</span>
</code></pre></div>
<ul>
<li>
<h5 id="_9">测试数据在面板展示</h5>
</li>
</ul>
<div class="highlight"><pre><span></span><code>宿主机服务器/var/log/messages做采集展示：
  1、新建loki数据源；
  2、explore匹配；
  3、run query；
</code></pre></div>
<p><img alt="登录面板" src="../image/grafana-sd-01.png" /></p>
<p><img alt="选择数据源" src="../image/grafana-loki-01.png" /></p>
<p><img alt="配置数据源" src="../image/grafana-loki-02.png" /></p>
<p><img alt="采集面板" src="../image/grafana-loki-show-01.png" /></p>
<p><img alt="数据采集展示" src="../image/grafana-loki-show-02.png" /></p>
<h4 id="k8sloki">测试环境k8s部署loki</h4>
<div class="highlight"><pre><span></span><code>注意：
k8s集群环境搭建这里不再赘述，可参考kubernetes环境搭建篇！
该操作涉及到的配置文件存放在loki_in_k8s目录！
这里只做了数据接入loki以及采集展示，做数据持久化不是该小结的重点，因此初学者使用此demo.yaml.bak_nostorage文件即可！
</code></pre></div>
<div class="highlight"><pre><span></span><code>[root@master-1 loki_in_k8s]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.100   master-1 server1
192.168.1.101   master-2 server2
192.168.1.102   master-3 server3

[root@master-1 loki_in_k8s]# ip addr | grep inet | grep ens
    inet 192.168.1.100/24 brd 192.168.1.255 scope global ens160
    inet 192.168.1.99/24 scope global secondary ens160  ## keepalived虚拟IP
[root@master-1 loki_in_k8s]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@master-1 loki_in_k8s]# kubectl get node
NAME       STATUS   ROLES                  AGE     VERSION
master-1   Ready    control-plane,master   5d12h   v1.22.1
master-2   Ready    &lt;none&gt;                 5d12h   v1.22.1
master-3   Ready    &lt;none&gt;                 5d12h   v1.22.1
[root@master-1 loki_in_k8s]# kubectl get pod -o wide
NAME                               READY   STATUS    RESTARTS        AGE     IP            NODE       NOMINATED NODE   READINESS GATES
alertmanager-0                     2/2     Running   2 (36h ago)     2d13h   10.244.2.9    master-3   &lt;none&gt;           &lt;none&gt;
alertmanager-1                     2/2     Running   2 (36h ago)     2d11h   10.244.1.22   master-2   &lt;none&gt;           &lt;none&gt;
alertmanager-2                     2/2     Running   2 (36h ago)     2d11h   10.244.1.27   master-2   &lt;none&gt;           &lt;none&gt;
demo-567b47d5f6-tf22j              2/2     Running   0               11h     10.244.1.30   master-2   &lt;none&gt;           &lt;none&gt;
grafana-0                          1/1     Running   4 (36h ago)     5d11h   10.244.1.24   master-2   &lt;none&gt;           &lt;none&gt;
loki-0                             1/1     Running   3 (36h ago)     5d11h   10.244.2.8    master-3   &lt;none&gt;           &lt;none&gt;
pvc-test-pod                       1/1     Running   4 (3h30m ago)   2d11h   10.244.1.28   master-2   &lt;none&gt;           &lt;none&gt;
test-deployment-7cff4666d5-8blw9   1/1     Running   1 (36h ago)     2d11h   10.244.2.7    master-3   &lt;none&gt;           &lt;none&gt;
[root@master-1 loki_in_k8s]# kubectl get svc
NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
alertmanager           NodePort    10.96.109.205    &lt;none&gt;        80:31575/TCP     2d13h
alertmanager-cluster   ClusterIP   None             &lt;none&gt;        9094/TCP         2d13h
demo                   NodePort    10.103.51.114    &lt;none&gt;        8080:31110/TCP   5d11h
glusterfs              ClusterIP   10.104.156.182   &lt;none&gt;        49152/TCP        11h
grafana                NodePort    10.101.93.163    &lt;none&gt;        3000:30710/TCP   5d11h
kubernetes             ClusterIP   10.96.0.1        &lt;none&gt;        443/TCP          5d12h
loki                   ClusterIP   None             &lt;none&gt;        3100/TCP         5d11h
loki-nodeport          NodePort    10.102.199.2     &lt;none&gt;        3100:32111/TCP   5d11h
test-deployment        NodePort    10.99.215.167    &lt;none&gt;        80:32000/TCP     2d11h

# pwd
/root/k8s/loki/loki_in_k8s
[root@master-1 loki_in_k8s]# ll
总用量 44
-rw-r--r-- 1 root root  605 4月   5 16:24 alertmanager-configmap.yaml
-rw-r--r-- 1 root root  754 4月   5 16:24 alertmanager-service.yaml
-rw-r--r-- 1 root root 2826 4月   5 16:24 alertmanager-sts.yaml
-rw-r--r-- 1 root root 1144 4月  11 23:04 demo.yaml      ## 存储到基于nfs的pv数据
-rw-r--r-- 1 root root 1078 4月  11 22:57 demo.yaml.bak_nostorage    ## 无持久化存储配置文件
-rw-r--r-- 1 root root 1472 4月   5 16:24 grafana-sts.yaml
-rw-r--r-- 1 root root   52 4月   5 16:24 labels.sh
-rw-r--r-- 1 root root 2473 4月   5 16:24 loki-configmap.yaml
-rw-r--r-- 1 root root  209 4月   5 16:24 loki-nodeport-svc.yaml
-rw-r--r-- 1 root root 1584 4月   5 16:24 loki-rules.yaml
-rw-r--r-- 1 root root 2727 4月   5 16:24 loki-server-sts.yaml

# kubectl get node --show-labels
NAME       STATUS   ROLES                  AGE     VERSION   LABELS
master-1   Ready    control-plane,master   5d12h   v1.22.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master-1,kubernetes.io/os=linux,loki=node,n
ode-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=master-2   Ready    &lt;none&gt;                 5d12h   v1.22.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master-2,kubernetes.io/os=linux,loki=node
master-3   Ready    &lt;none&gt;                 5d12h   v1.22.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master-3,kubernetes.io/os=linux,loki=node

这里仅作数据源-Loki配置及其数据展示，告警邮件发送等后续补充！
</code></pre></div>
<div class="highlight"><pre><span></span><code># 打标签
kubectl label node master-1  loki=node --overwrite
kubectl label node master-2  loki=node --overwrite
kubectl label node master-3  loki=node --overwrite
# 启动loki
kubectl apply -f loki-server-sts.yaml -f loki-configmap.yaml -f loki-nodeport-svc.yaml -f loki-rules.yaml
# 启动grafana
kubectl apply -f grafana-sts.yaml
# 启动sidecar应用程序，初学者请将demo.yaml.bak_nostorage命名为demo.yaml执行即可
kubectl apply -f demo.yaml
</code></pre></div>
<p><img alt="数据源配置" src="../image/k8s-grafana-loki-01.png" /></p>
<p><img alt="日志采集页面" src="../image/k8s-grafana-loki-show-01.png" /></p>
<p><img alt="日志查看" src="../image/k8s-grafana-loki-show-02.png" /></p>
<div class="highlight"><pre><span></span><code>至此，一个sidecar应用的日志采集就展示完成了！
</code></pre></div>
<h3 id="_10">数据持久化配置</h3>
<h4 id="_11">方案说明</h4>
<div class="highlight"><pre><span></span><code>集群基于glusterfs和nfs共享的pv及pvc操作，建议直接使用glusterfs

这里侧重对pv和pvc的分配，以及应用业务日志的挂载配置！

两个环境的具体搭建及其使用参考数据持久化相关参考文档！
</code></pre></div>
<div class="highlight"><pre><span></span><code>## 文件存放在loki_in_k8s中的pv目录
# pwd
/root/k8s/pv
[root@master-1 pv]# ll
总用量 16
-rw-r--r-- 1 root root 187 4月  11 23:08 demo-pvc.yml
-rw-r--r-- 1 root root 219 4月  11 22:53 endpoints.yaml
-rw-r--r-- 1 root root 271 4月  11 22:55 pv.yaml
-rw-r--r-- 1 root root 199 4月  11 22:54 service.yam
</code></pre></div>
<div class="highlight"><pre><span></span><code># kubectl get pv
NAME        CAPACITY    ACCESS MODES    RECLAIM POLICY  STATUS  CLAIM   STORAGECLASS    REASON  AGE
demo-pv 1Gi             RWX                     Retain                  Available                                                       15h
# kubectl get pvc
  NAME      STATUS  VOLUME                                                                      CAPACITY    ACCESS  MODES   STORAGECLASS    AGE
demo-pvc    Bound       pvc-4567db81-67ee-4bee-8417-8d014c42f8ac    200m       RWX    managed-nfs-storage           14h
## 如下可以看到，将pod中sidecar日志都挂载到了当前的pv存储中
# ll /data/nfs-volume/prometheus/default-demo-pvc-pvc-4567db81-67ee-4bee-8417-8d014c42f8ac/
总用量 28
-rw-r--r-- 1 root root 6135 4月  11 23:09 catalina.2023-04-11.log
-rw-r--r-- 1 root root 7007 4月  12 10:44 catalina.2023-04-12.log
-rw-r--r-- 1 root root    0 4月  11 23:08 host-manager.2023-04-11.log
-rw-r--r-- 1 root root    0 4月  12 10:44 host-manager.2023-04-12.log
-rw-r--r-- 1 root root  459 4月  11 23:09 localhost.2023-04-11.log
-rw-r--r-- 1 root root  735 4月  12 10:44 localhost.2023-04-12.log
-rw-r--r-- 1 root root    0 4月  11 23:08 localhost_access_log.2023-04-11.txt
-rw-r--r-- 1 root root    0 4月  12 10:44 localhost_access_log.2023-04-12.txt
-rw------- 1 root root  171 4月  12 14:06 loki_positions.yaml
-rw-r--r-- 1 root root    0 4月  11 23:08 manager.2023-04-11.log
-rw-r--r-- 1 root root    0 4月  12 10:44 manager.2023-04-12.log
</code></pre></div>
<h3 id="_12">日志监控告警……待补充</h3>
<h3 id="_13">官网参考链接</h3>
<div class="highlight"><pre><span></span><code>容器部署loki:
https://grafana.com/docs/loki/latest/installation/docker/

容器部署grafana:
https://grafana.com/docs/grafana/latest/setup-grafana/installation/docker/

GitHub参考：
https://github.com/grafana/loki/releases
</code></pre></div>
<h3 id="_14">参考引用</h3>
<div class="highlight"><pre><span></span><code>第二种-sidecar模式日志收集
https://www.cnblogs.com/huningfei/p/17122929.html

多集群thanos sidecar+MinIO监控告警实践
https://blog.51cto.com/starsliao/5763149

promtail收集标准输出日志
https://www.isolves.com/it/wlyx/fwq/2021-07-30/42209.html


Linux搭建Promtail + Loki + Grafana 轻量日志监控系统
https://blog.csdn.net/weixin_52270081/article/details/124801308?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-124801308-blog-111883107.235^v27^pc_relevant_recovery_v2&amp;spm=1001.2101.3001.4242.1&amp;utm_relevant_index=2

k8s安装loki-promtail（非helm安装）
https://www.cnblogs.com/danny-djy/p/15523349.html

Loki &amp; Promtail 详解
https://cloud.tencent.com/developer/article/1889851?from=15425&amp;areaSource=102001.2&amp;traceId=h1E1q5tpdQD1DSEHFau4A


部署grafana:
https://blog.csdn.net/u011943534/article/details/102906030
部署loki:
https://blog.csdn.net/u011943534/article/details/120871258?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-4-120871258-blog-112691053.235^v27^pc_relevant_recovery_v2&amp;spm=1001.2101.3001.4242.3&amp;utm_relevant_index=7

loki数据存储相关：
https://blog.51cto.com/u_15820126/5738632

京东到家Loki日志系统实践
https://mp.weixin.qq.com/s/-IvWfhXbm3WXFIBtbrhX-A

使用 loki 作为 k8s 应用日志收集器(下篇)
https://blog.cong.moe/post/2020-08-08-use_loki_as_k8s_log_collector_2/
https://gist.githubusercontent.com/zcong1993/2ed197b97a3286dd958e4c8cfd81e5ea/raw/2c8504b5f1e3aecf9c874c637be1d5c774c936a3/loki2.yaml

含AWS的部分配置：
https://blog.csdn.net/u014331650/article/details/127889779
https://grafana.com/docs/loki/latest/configuration/examples/

官方loki生产环境集群kubernetes方案
https://github.com/LinkMaq/loki-cluster-deploy/tree/master/production/loki-system

存储示例：
https://blog.csdn.net/Java_Mr_Jin/article/details/125643455?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168076003416800197087695%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&amp;request_id=168076003416800197087695&amp;biz_id=0&amp;utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-125643455-null-null.142^v81^pc_new_rank,201^v4^add_ask,239^v2^insert_chatgpt&amp;utm_term=minio&amp;spm=1018.2226.3001.4187
</code></pre></div>
<h3 id="_15">历史事件</h3>
<h4 id="2023-04-12">2023-04-12｜容器、虚拟机轻量部署采集</h4>
<h4 id="2023-04-10-k8s">2023-04-10 ｜k8s轻量部署采集</h4>
<h4 id="2023-04-08">2023-04-08 ｜创建文档</h4>


  




                
              </article>
            </div>
          
          
        </div>
        
          <button type="button" class="md-top md-icon" data-md-component="top" hidden>
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8v12Z"/></svg>
            Back to top
          </button>
        
      </main>
      
        <footer class="md-footer">
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
    <div class="md-copyright__highlight">
      Copyright © 2022 <a href="http://happyhelloworld.gitee.io/blog">Powered by http://happyhelloworld.gitee.io 版权所有</a>
    </div>
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
        <div class="md-social">
  
    
    
    
    
      
      
    
    <a href="https://github.com" target="_blank" rel="noopener" title="github.com" class="md-social__link">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg>
    </a>
  
    
    
    
    
      
      
    
    <a href="https://hub.docker.com" target="_blank" rel="noopener" title="hub.docker.com" class="md-social__link">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M349.9 236.3h-66.1v-59.4h66.1v59.4zm0-204.3h-66.1v60.7h66.1V32zm78.2 144.8H362v59.4h66.1v-59.4zm-156.3-72.1h-66.1v60.1h66.1v-60.1zm78.1 0h-66.1v60.1h66.1v-60.1zm276.8 100c-14.4-9.7-47.6-13.2-73.1-8.4-3.3-24-16.7-44.9-41.1-63.7l-14-9.3-9.3 14c-18.4 27.8-23.4 73.6-3.7 103.8-8.7 4.7-25.8 11.1-48.4 10.7H2.4c-8.7 50.8 5.8 116.8 44 162.1 37.1 43.9 92.7 66.2 165.4 66.2 157.4 0 273.9-72.5 328.4-204.2 21.4.4 67.6.1 91.3-45.2 1.5-2.5 6.6-13.2 8.5-17.1l-13.3-8.9zm-511.1-27.9h-66v59.4h66.1v-59.4zm78.1 0h-66.1v59.4h66.1v-59.4zm78.1 0h-66.1v59.4h66.1v-59.4zm-78.1-72.1h-66.1v60.1h66.1v-60.1z"/></svg>
    </a>
  
    
    
    
    
      
      
    
    <a href="https://pypi.org/project/mkdocs-material/" target="_blank" rel="noopener" title="pypi.org" class="md-social__link">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M439.8 200.5c-7.7-30.9-22.3-54.2-53.4-54.2h-40.1v47.4c0 36.8-31.2 67.8-66.8 67.8H172.7c-29.2 0-53.4 25-53.4 54.3v101.8c0 29 25.2 46 53.4 54.3 33.8 9.9 66.3 11.7 106.8 0 26.9-7.8 53.4-23.5 53.4-54.3v-40.7H226.2v-13.6h160.2c31.1 0 42.6-21.7 53.4-54.2 11.2-33.5 10.7-65.7 0-108.6zM286.2 404c11.1 0 20.1 9.1 20.1 20.3 0 11.3-9 20.4-20.1 20.4-11 0-20.1-9.2-20.1-20.4.1-11.3 9.1-20.3 20.1-20.3zM167.8 248.1h106.8c29.7 0 53.4-24.5 53.4-54.3V91.9c0-29-24.4-50.7-53.4-55.6-35.8-5.9-74.7-5.6-106.8.1-45.2 8-53.4 24.7-53.4 55.6v40.7h106.9v13.6h-147c-31.1 0-58.3 18.7-66.8 54.2-9.8 40.7-10.2 66.1 0 108.6 7.6 31.6 25.7 54.2 56.8 54.2H101v-48.8c0-35.3 30.5-66.4 66.8-66.4zm-6.7-142.6c-11.1 0-20.1-9.1-20.1-20.3.1-11.3 9-20.4 20.1-20.4 11 0 20.1 9.2 20.1 20.4s-9 20.3-20.1 20.3z"/></svg>
    </a>
  
    
    
    
    
      
      
    
    <a href="https://www.bilibili.com/" target="_blank" rel="noopener" title="www.bilibili.com" class="md-social__link">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M488.6 104.1c16.7 18.1 24.4 39.7 23.3 65.7v202.4c-.4 26.4-9.2 48.1-26.5 65.1-17.2 17-39.1 25.9-65.5 26.7H92.02c-26.45-.8-48.21-9.8-65.28-27.2C9.682 419.4.767 396.5 0 368.2V169.8c.767-26 9.682-47.6 26.74-65.7C43.81 87.75 65.57 78.77 92.02 78h29.38L96.05 52.19c-5.75-5.73-8.63-13-8.63-21.79 0-8.8 2.88-16.06 8.63-21.797C101.8 2.868 109.1 0 117.9 0s16.1 2.868 21.9 8.603L213.1 78h88l74.5-69.397C381.7 2.868 389.2 0 398 0c8.8 0 16.1 2.868 21.9 8.603 5.7 5.737 8.6 12.997 8.6 21.797 0 8.79-2.9 16.06-8.6 21.79L394.6 78h29.3c26.4.77 48 9.75 64.7 26.1zm-38.8 69.7c-.4-9.6-3.7-17.4-10.7-23.5-5.2-6.1-14-9.4-22.7-9.8H96.05c-9.59.4-17.45 3.7-23.58 9.8-6.14 6.1-9.4 13.9-9.78 23.5v194.4c0 9.2 3.26 17 9.78 23.5s14.38 9.8 23.58 9.8H416.4c9.2 0 17-3.3 23.3-9.8 6.3-6.5 9.7-14.3 10.1-23.5V173.8zm-264.3 42.7c6.3 6.3 9.7 14.1 10.1 23.2V273c-.4 9.2-3.7 16.9-9.8 23.2-6.2 6.3-14 9.5-23.6 9.5-9.6 0-17.5-3.2-23.6-9.5-6.1-6.3-9.4-14-9.8-23.2v-33.3c.4-9.1 3.8-16.9 10.1-23.2 6.3-6.3 13.2-9.6 23.3-10 9.2.4 17 3.7 23.3 10zm191.5 0c6.3 6.3 9.7 14.1 10.1 23.2V273c-.4 9.2-3.7 16.9-9.8 23.2-6.1 6.3-14 9.5-23.6 9.5-9.6 0-17.4-3.2-23.6-9.5-7-6.3-9.4-14-9.7-23.2v-33.3c.3-9.1 3.7-16.9 10-23.2 6.3-6.3 14.1-9.6 23.3-10 9.2.4 17 3.7 23.3 10z"/></svg>
    </a>
  
    
    
    
    
    <a href="mailto:<xxxxxx@qq.com>" target="_blank" rel="noopener" title="" class="md-social__link">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M498.1 5.6c10.1 7 15.4 19.1 13.5 31.2l-64 416c-1.5 9.7-7.4 18.2-16 23s-18.9 5.4-28 1.6L284 427.7l-68.5 74.1c-8.9 9.7-22.9 12.9-35.2 8.1S160 493.2 160 480v-83.6c0-4 1.5-7.8 4.2-10.7l167.6-182.9c5.8-6.3 5.6-16-.4-22s-15.7-6.4-22-.7L106 360.8l-88.3-44.2C7.1 311.3.3 300.7 0 288.9s5.9-22.8 16.1-28.7l448-256c10.7-6.1 23.9-5.5 34 1.4z"/></svg>
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    
    <script id="__config" type="application/json">{"base": "../../..", "features": ["navigation.tracking", "navigation.tabs", "navigation.tabs.sticky", "navigation.sections", "navigation.expand", "navigation.indexes", "toc.integrate", "toc.integrate", "navigation.top", "header.autohide", "search.share", "search.suggest", "search.highlight"], "search": "../../../assets/javascripts/workers/search.208ed371.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script>
    
    
      <script src="../../../assets/javascripts/bundle.51198bba.min.js"></script>
      
    
  </body>
</html>